package springboot.file.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import springboot.common_utils.R;
import springboot.file.utils.MinioUtils;

import java.util.Map;

/**
 * @author daiqiang
 * @since 2024/7/12-22:02
 *
 * Spring Boot集成Minl0开发
 * MinioClient的常用API:
 * putObject（):用于上传文件到指定的存储桶,
 * statObject（）:用于检查指定的对象(文件)的状态;
 * getobjectUrl（）:用于生成一个对象(文件)的URL，以便可以通过HTTP访问;
 * getObject（）:用于从指定的存储桶中下载文件;
 * listobjects（）:用于列出指定存储桶中的所有对象(文件);>
 * removeObject（）:用于删除指定存储桶中的对象，需要指定存储桶名称和对象键
 */
@Slf4j
@CrossOrigin  //允许跨域
@RestController
@RequestMapping("file")
public class FileController {
    @Autowired
    private MinioUtils minioUtils;

    @PostMapping("/upload")
    public ResponseEntity<Map<String, Object>> uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            Map<String, Object> result = minioUtils.upload(file);
            if (result != null) {
                // 假设上传成功，result中包含了"fileName"
                String fileName = (String) result.get("fileName");
                String url = minioUtils.getUrl(fileName);
                if (url != null) {
                    // 可以在result中添加URL，或者返回一个新的包含URL的Map
                    result.put("url", url);
                    return new ResponseEntity<>(result, HttpStatus.OK);
                } else {
                    // 处理URL生成失败的情况
                    result.put("error", "URL generation failed");
                    return new ResponseEntity<>(result, HttpStatus.INTERNAL_SERVER_ERROR);
                }
            }
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    //get  url下载
    @GetMapping("/download")
    //public ResponseEntity<String> getFileUrl(@PathVariable String fileName) {
    public R getFileUrl(@RequestParam String fileName) {
        try {
            String url = minioUtils.getUrl(fileName);
            if (url != null && !url.isEmpty()) {
                String msg = HttpStatus.OK.toString();
                return R.success(2000,msg,url);
            } else {
                return R.fail(2003,"File not found");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping("/download/{fileName}/{expiry}")
    public ResponseEntity<String> getFileUrlWithExpiry(@PathVariable String fileName, @PathVariable Integer expiry) {
        try {
            String url = minioUtils.getUrl(fileName, expiry);
            if (url != null) {
                return new ResponseEntity<>(url, HttpStatus.OK);
            } else {
                return new ResponseEntity<>(HttpStatus.NOT_FOUND);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @DeleteMapping("/delete/{fileName}")
    public ResponseEntity<Void> deleteFile(@PathVariable String fileName) {
        try {
            minioUtils.delete(fileName);
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


}
